package com.yuncheng.mcp.tool;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Map;

/**
 * 基于SpEL表达式的简单动态工具实现
 */
public class SimpleDynamicTool implements DynamicTool {

    private final String name;
    private final String description;
    private final String expression;
    private final Map<String, Class<?>> parameterTypes;

    public SimpleDynamicTool(String name, String description, String expression,
                            Map<String, Class<?>> parameterTypes) {
        this.name = name;
        this.description = description;
        this.expression = expression;
        this.parameterTypes = parameterTypes;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public Map<String, Class<?>> getParameterTypes() {
        return parameterTypes;
    }

    @Override
    public Object execute(Map<String, Object> arguments) {
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();

        // 注册数学函数
        try {
            context.registerFunction("pow", Math.class.getDeclaredMethod("pow", double.class, double.class));
            context.registerFunction("sqrt", Math.class.getDeclaredMethod("sqrt", double.class));
            context.registerFunction("sin", Math.class.getDeclaredMethod("sin", double.class));
            context.registerFunction("cos", Math.class.getDeclaredMethod("cos", double.class));
            context.registerFunction("tan", Math.class.getDeclaredMethod("tan", double.class));
            context.registerFunction("abs", Math.class.getDeclaredMethod("abs", double.class));
            context.registerFunction("round", Math.class.getDeclaredMethod("round", double.class));
            context.registerFunction("floor", Math.class.getDeclaredMethod("floor", double.class));
            context.registerFunction("ceil", Math.class.getDeclaredMethod("ceil", double.class));
            context.registerFunction("max", Math.class.getDeclaredMethod("max", double.class, double.class));
            context.registerFunction("min", Math.class.getDeclaredMethod("min", double.class, double.class));
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 将参数添加到上下文
        arguments.forEach(context::setVariable);

        // 处理表达式，将Math.xxx替换为#xxx
        String processedExpression = expression;
        if (expression.contains("Math.")) {
            processedExpression = expression.replace("Math.pow", "#pow")
                    .replace("Math.sqrt", "#sqrt")
                    .replace("Math.sin", "#sin")
                    .replace("Math.cos", "#cos")
                    .replace("Math.tan", "#tan")
                    .replace("Math.abs", "#abs")
                    .replace("Math.round", "#round")
                    .replace("Math.floor", "#floor")
                    .replace("Math.ceil", "#ceil")
                    .replace("Math.max", "#max")
                    .replace("Math.min", "#min");
        }

        System.out.println("原始表达式: " + expression);
        System.out.println("处理后表达式: " + processedExpression);

        Expression exp = parser.parseExpression(processedExpression);
        return exp.getValue(context);
    }

}
